package car;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class UpdateServlet extends HttpServlet {
	
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
	perform(req,resp);
	}
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
	perform(req,resp);
	
	}
	private void perform(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException{
		req.setCharacterEncoding("UTF-8");
		String strIndex = req.getParameter("index");
		String number = req.getParameter("number");
		String name = req.getParameter("name");
		String maker = req.getParameter("maker");
		System.out.println(number);
		System.out.println(name);
		System.out.println(maker);
		//Carオブジェクトを生成する
		int index = Integer.parseInt(strIndex);
		Car car =CarList.get(index);
		if(number.matches("[0-9]{4}")){//そのパターンにマッチしているか見る
			car.setNumber(number);
		}
		car.setName(name);
		car.setMaker(maker);	
		//一覧表を表示する（画面遷移）
		resp.sendRedirect("index.jsp");
	}

}
